﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iHub.App_Code;

namespace iHub
{
    public partial class forgetQuest : Form
    {
        private AccountManager acct = new AccountManager();
        private String username;
        private String q;

        public forgetQuest(String question)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);

            username = forgetStart.form1.getUsername();

            q = acct.getQuestion(username);

            secQuest.Text = q;
        }

        public string RandomString()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(chars, 8)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());

            return result.ToString();
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                forgetStart n = new forgetStart();
                n.Show();
            }
        }

        private void next_Click(object sender, EventArgs e)
        {
            string newPassword;
            string answer = secAns.Text;
            Boolean verified = false;
            StringValidator validator = new StringValidator();
            verified = acct.verifyAnswer(q, username, answer);

            try
            {
                if (validator.IsSafeString(answer))
                {
                    if (verified)
                    {
                        newPassword = RandomString();
                        this.Dispose();

                        DialogResult msg = MessageBox.Show("Your new password is " + newPassword + ". Please login with your new password.", "Forget Password", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        acct.editPassword(username, newPassword);
                        login view = new login();
                        view.ShowDialog();
                    }
                    else if (answer == "")
                    {
                        DialogResult msg = MessageBox.Show("Please make sure that you have entered you security answer.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {
                        DialogResult msg = MessageBox.Show("The security answer does not match with your security question.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"Fatal",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }
        }

        private void back_Click(object sender, EventArgs e)
        {
            forgetStart n = new forgetStart();
            n.Show();
            this.Dispose();
        }
    }
}
